/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author diegomanguia
 */
public class Conexion {

    private String bd;
    private String user;
    private String pass;

    public Connection conectar() throws SQLException {
        Connection cn = null;
        bd = "jdbc:mysql://localhost/publicont";
        user = "root";
        pass = "diego";
        try {
            Properties p=leerIni();
            bd=p.getProperty("bd", bd);
            user=p.getProperty("user", user);
            pass=p.getProperty("pass", pass);
            Class.forName("com.mysql.jdbc.Driver");
            cn = DriverManager.getConnection(bd, user, pass);

        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error en el Driver de la Base de Datos.");
        }
        return cn;
    }
    public Properties leerIni() {
        Properties p;
            p=new Properties();
        try {
            FileInputStream archivo =new FileInputStream("./bd.ini");
            p.load(archivo);
            archivo.close();
        } catch (IOException ex) {
            System.out.println("No se pudo leer el fichero que contiene "
                    + "la configuracion de la base de datos");
            
        }
        return p;
    }
    public void desconectar(Connection cn) {
        try {
            cn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error al desconectar BD: " + ex.getMessage());
        }
        cn = null;

    }

    public void deshacer(Connection cn) {
        try {
            cn.rollback();
        } catch (SQLException e) {
            System.out.println("Error. No hemos podido deshacer." + e.getMessage());
        }
    }
}
